package pl.krzosekm.vehicleregister.backup;
import java.awt.BorderLayout;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;

import javax.swing.WindowConstants;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class HeaderPanel extends JPanel implements Runnable {
	
	

	private static final long serialVersionUID = -8197858251734304955L;
	private JToolBar jToolBar1;
	private JButton jButton1;
	private JLabel jLabel11;
	private JLabel timeLabel;
	private JLabel dateLabel;
	private JPanel jPanel7;
	private JLabel jLabel1;
	private JPanel jPanel2;
	private JPanel jPanel1;
	private JButton jButton4;
	private JButton jButton3;
	private JButton jButton2;
	private Thread timer;
	
	public final static String NEW = "NEW";
	public final static String EDIT = "EDIT";
	

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new HeaderPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public HeaderPanel() {
		super();
		initGUI();
		start();
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			this.setPreferredSize(new Dimension(849, 89));
			{
				jToolBar1 = new JToolBar();
				this.add(jToolBar1, BorderLayout.CENTER);
				{
					jButton1 = new JButton();
					jToolBar1.add(jButton1);
					jButton1.setName("jButton1");
					jButton1.setMaximumSize(new Dimension(100, 100));
					jButton1.setAction(getAppActionMap().get("abstractAction1"));
					jButton1.setPreferredSize(new Dimension(81, 85));
					jButton1.setAction(getAppActionMap().get("addRepair"));
					jButton1.addKeyListener(new KeyAdapter() {
						public void keyPressed(KeyEvent evt) {
							jButton1KeyPressed(evt);
						}
					});
				}
				{
					jButton2 = new JButton();
					jToolBar1.add(jButton2);
					jButton2.setMaximumSize(new Dimension(100, 100));
					jButton2.setName("jButton2");
					jButton2.setPreferredSize(new Dimension(79, 85));
				}
				{
					jButton3 = new JButton();
					jToolBar1.add(jButton3);
					jButton3.setMaximumSize(new Dimension(100, 100));
					jButton3.setName("jButton3");
					jButton3.setPreferredSize(new Dimension(84, 85));
				}
				{
					jButton4 = new JButton();
					jToolBar1.add(jButton4);
					jButton4.setMaximumSize(new Dimension(100, 100));
					jButton4.setName("jButton4");
					jButton4.setPreferredSize(new Dimension(90, 85));
				}
				{
					jPanel1 = new JPanel();
					BorderLayout jPanel1Layout = new BorderLayout();
					jPanel1.setLayout(jPanel1Layout);
					jToolBar1.add(jPanel1);
					jPanel1.setPreferredSize(new Dimension(527, 85));
					{
						jPanel2 = new JPanel();
						BorderLayout jPanel2Layout = new BorderLayout();
						jPanel2.setLayout(jPanel2Layout);
						jPanel1.add(jPanel2, BorderLayout.EAST);
						jPanel2.setPreferredSize(new java.awt.Dimension(230, 85));
						{
							jLabel1 = new JLabel();
							jPanel2.add(jLabel1, BorderLayout.WEST);
							jLabel1.setName("jLabel1");
							jLabel1.setPreferredSize(new java.awt.Dimension(92, 85));
						}
						{
							jPanel7 = new JPanel();
							jPanel2.add(jPanel7, BorderLayout.CENTER);
							GridLayout jPanel7Layout = new GridLayout(3, 1);
							jPanel7Layout.setColumns(1);
							jPanel7Layout.setRows(3);
							jPanel7.setLayout(jPanel7Layout);
							{
								dateLabel = new JLabel();
								jPanel7.add(dateLabel);
								dateLabel.setName("dateLabel");
								dateLabel.setPreferredSize(new java.awt.Dimension(151, 28));
							}
							{
								timeLabel = new JLabel();
								jPanel7.add(timeLabel);
								timeLabel.setName("timeLabel");
								timeLabel.setPreferredSize(new java.awt.Dimension(160, 28));
							}
							{
								jLabel11 = new JLabel();
								jPanel7.add(jLabel11);
								jLabel11.setName("jLabel11");
							}
						}
					}
				}
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void start() {
		if (timer == null) { // If we don't already have a thread
			timer = new Thread(this); // Then create one
			timer.start(); // And start it running
		}
	}

	public void run() {
		while (true) { // Loop until we're stopped
			DateFormat dateFormat = new SimpleDateFormat("d MMM yyyy (EEE)");
			DateFormat timeFormat = new SimpleDateFormat("mm:ss");
			Date nowIs = new Date();
			dateLabel.setText(dateFormat.format(nowIs));
			timeLabel.setText(timeFormat.format(nowIs));

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}

		}

	}
	
	
    /**
    * Returns the action map used by this application.
     * Actions defined using the Action annotation
     * are returned by this method
     */
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}
	
	@Action
	public void addRepair() {
		RepairFrame repairFrame = new RepairFrame(NEW);
		repairFrame.setVisible(true);
	}
	
	private void jButton1KeyPressed(KeyEvent evt) {
//		System.out.println("jButton1.keyPressed, event="+evt);
//		System.out.println(evt.getModifiers());
		if (evt.getModifiers() == 2 && evt.getKeyCode() == 78)
		{
			addRepair();
		}
		//TODO add your code for jButton1.keyPressed
	}

}
